<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>用户中心</title>

    <script src="/ss/static/js/jquery/jquery-2.1.4.min.js" type="text/javascript"></script>
    <script src="/ss/static/layui/layui.js" type="text/javascript"></script>
    <script src="/ss/static/js/index/index.js" type="text/javascript"></script>
    <script src="/ss/static/layui/lay/modules/layer.js" type="text/javascript"></script>
    <script src="/ss/static/js/index/sliders.js" type="text/javascript"></script>
    <script src="/ss/static/js/index/html5.js" type="text/javascript"></script>
    <script src="/ss/static/js/index/freezeheader.js" type="text/javascript"></script>
    <script src="/ss/static/js/crypto-js.js" type="text/javascript"></script>
    <link rel="stylesheet" href="/ss/static/layui/css/layui.css" media="all"/>
    <link rel="stylesheet" href="/ss/static/layui/css/modules/layer/default/layer.css"/>
    <link rel="stylesheet" href="/ss/static/css/global.css"/>
</head>
<body>
<div th:replace="template.html::header"></div>

<div class="layui-container container" style="padding-top:70px;">
    <div class="main fly-user-main layui-clear">
        <ul class="layui-nav layui-nav-tree layui-inline" lay-filter="user">
            <li class="layui-nav-item layui-this">
                <a th:href="'center?uid=' + ${user.getUid()}">
                    <i class="layui-icon">&#xe612;</i>
                    用户中心
                </a>
            </li>
            <!--            <li class="layui-nav-item">
                            <a th:href="'setting?uid=' + ${user.getUid()}">
                                <i class="layui-icon">&#xe620;</i>
                                基本设置
                            </a>
                        </li>-->
            <li class="layui-nav-item">
                <a th:href="'message?uid=' + ${user.getUid()}">
                    <i class="layui-icon">&#xe611;</i>
                    我的消息
                </a>
            </li>
        </ul>

        <div class="fly-panel fly-panel-user">
            <div class="layui-tab layui-tab-brief" lay-filter="user">
                <ul class="layui-tab-title" id="LAY_mine">
                    <li class="layui-this" lay-id="info">我的资料</li>
                    <li lay-id="avatar">头像</li>
                    <li lay-id="pass">密码</li>
                </ul>
                <div class="layui-tab-content" style="padding: 20px 0;">
                    <div class="layui-form layui-form-pane layui-tab-item layui-show">
                        <div class="layui-form">
                            <div class="layui-form-item">
                                <label class="layui-form-label">账号</label>
                                <div class="layui-input-inline">
                                    <div class="layui-input" th:text="${user.getAccount()}">
                                    </div>
                                </div>
                            </div>
                            <div class="layui-form-item">
                                <label class="layui-form-label">用户名</label>
                                <div class="layui-input-inline">
                                    <div th:text="${user.getName()}" class="layui-input">
                                    </div>
                                </div>
                            </div>
                            <div class="layui-form-item">
                                <label class="layui-form-label">角色</label>
                                <div class="layui-input-inline">
                                    <div th:text="${user.getRole()}" class="layui-input">
                                    </div>
                                </div>
                            </div>
                            <div class="layui-form-item">
                                <label class="layui-form-label">电话</label>
                                <div class="layui-input-inline">
                                    <div th:text="${user.getPhone()}" class="layui-input">
                                    </div>
                                </div>
                            </div>
                            <div class="layui-form-item">
                                <label class="layui-form-label">QQ</label>
                                <div class="layui-input-inline">
                                    <div th:text="${user.getQq()}" class="layui-input">
                                    </div>
                                </div>
                            </div>
                            <div class="layui-form-item">
                                <label class="layui-form-label">出生日期</label>
                                <div class="layui-input-inline">
                                    <div id="birth"
                                         th:text="${#dates.format(user.getBirth(), 'yyyy-MM-dd')}"
                                         class="layui-input">
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="layui-form layui-form-pane layui-tab-item">
                        <div class="layui-form-item">
                            <label for="L_nowpass" class="layui-form-label">头像网址</label>
                            <div class="layui-input-inline">
                                <input type="text" lay-verify="required" autocomplete="off" class="layui-input"
                                       id="face">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <button class="layui-btn" lay-filter="*"
                                    th:onclick="'uploadFace(' + ${user.getUid()} + ')'">确认修改
                            </button>
                        </div>
                    </div>
                    <div class="layui-form layui-form-pane layui-tab-item">
                        <div class="layui-form">
                            <div class="layui-form-item">
                                <label for="L_nowpass" class="layui-form-label">当前密码</label>
                                <div class="layui-input-inline">
                                    <input type="password" id="L_nowpass" lay-verify="required"
                                           autocomplete="off" class="layui-input">
                                </div>
                            </div>
                            <div class="layui-form-item">
                                <label for="L_pass" class="layui-form-label">新密码</label>
                                <div class="layui-input-inline">
                                    <input type="password" id="L_pass" lay-verify="required"
                                           autocomplete="off" class="layui-input">
                                </div>
                                <div class="layui-form-mid layui-word-aux">6到16个字符</div>
                            </div>
                            <div class="layui-form-item">
                                <label for="L_repass" class="layui-form-label">确认密码</label>
                                <div class="layui-input-inline">
                                    <input type="password" id="L_repass" lay-verify="required"
                                           autocomplete="off" class="layui-input">
                                </div>
                            </div>
                            <div class="layui-form-item">
                                <button class="layui-btn" lay-filter="*"
                                        th:onclick="'modify(' + ${user.getUid()} + ')'">确认修改
                                </button>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<div th:replace="template.html::footer"></div>

<script type="text/javascript">
    function modify(uid) {
        var nowpass = document.getElementById('L_nowpass').value
        $.ajax({
            url: '/ss/api/user/' + uid,
            method: 'get',
            success: function (user) {
                if (user['password'] !== CryptoJS.SHA1(nowpass).toString()) {
                    alert('当前密码错误,请重新输入!')
                    return
                }
                var pass = document.getElementById('L_pass').value
                var repass = document.getElementById('L_repass').value
                if (pass !== repass) {
                    alert('两次密码输入不一致,请重新输入!')
                    return
                }
                user['password'] = CryptoJS.SHA1(pass).toString()
                $.ajax({
                    url: '/ss/api/user',
                    method: 'put',
                    data: JSON.stringify(user),
                    dataType: 'json',
                    contentType: "application/json;charset=utf-8",
                    success: function (result) {
                        if (result['success']) location.reload()
                        else alert('修改密码失败!')
                    }
                })
            }
        })
    }

    function uploadFace(uid) {
        $.ajax({
            url: '/ss/api/user/' + uid,
            method: 'get',
            success: function (user) {
                user['face'] = document.getElementById('face').value
                $.ajax({
                    url: '/ss/api/user',
                    method: 'put',
                    data: JSON.stringify(user),
                    dataType: 'json',
                    contentType: "application/json;charset=utf-8",
                    success: function (result) {
                        if (result['success']) location.reload()
                        else alert('修改头像失败!')
                    }
                })
            }
        })
    }
</script>
</body>
</html>